package main.flex.component.main
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	
	import main.flex.conf.MainConf;
	
	import mx.controls.Label;
	import mx.events.FlexEvent;
	import mx.styles.ISimpleStyleClient;

	public class HeaderUserLabel extends Label
	{
		private var _headerUserLabelSkin		: DisplayObject;
		
		public function HeaderUserLabel()
		{
			super();
			styleName = MainConf.USER_NAME_FONT;
			addEventListener( FlexEvent.INITIALIZE, createChild );
		}

		override protected function updateDisplayList(w:Number, h:Number):void
		{
			super.updateDisplayList(w, h);
			
			// if the _headerBgSkin implements the basic style client properties & functions, set his style appropriately
			if( _headerUserLabelSkin is ISimpleStyleClient )
				ISimpleStyleClient( _headerUserLabelSkin ).styleName = this;
			
			var w2 : Number = w+10;
			
			_headerUserLabelSkin.width 	= w2;
			_headerUserLabelSkin.height = h;
			_headerUserLabelSkin.x 		= ( w - w2 ) / 2;
			_headerUserLabelSkin.y 		= ( _headerUserLabelSkin.height - h ) / 2;
		}
		
		private function createChild( pEvt : FlexEvent ) : void
		{
			// get the reference to the style defined in the CSS
            var Skin	: Object = getStyle("labelSkin");
            
            // if the style is a Sprite (therefore a symbol in a .swf)
            if( Skin is DisplayObject )
            {
                // nothing fancy here, just use it straight
                _headerUserLabelSkin = Skin as DisplayObject;

                // if it is a sprite, make sure it doesn't react to mouse events
                if( _headerUserLabelSkin is Sprite )
                    ( Skin as Sprite ).mouseEnabled = false;
            }
            // however if it is a class reference
            else if( Skin is Class )
            {
                // instantiate it, assuming it is derived from ProgrammaticSkin
                _headerUserLabelSkin = new Skin();
            }
            // finally, add it to the displayList   
            addChildAt( _headerUserLabelSkin, 1  );
            swapChildrenAt( 1, 0 );
		}
	}
}